﻿using GeneralLibrary;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using System.Linq;


namespace PersonalWeb.Controller
{
    [Route("api/[controller]")]
    [ApiController]
    public class FileNameController : ControllerBase
    {
        [HttpGet("{model}")]
        public IEnumerable<string> Get(string model) // 获取静态文件夹下的所有图片文件
        {
            List<string> imageUrls = new List<string>();
            try
            {
                string path = $"{Environment.CurrentDirectory}\\wwwroot\\Images\\{model}\\";
                string[] files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                .Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".webp", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
                .ToArray();

                foreach (var file in files)
                {
                    string fileName = file.Replace($"{Environment.CurrentDirectory}\\wwwroot\\Images\\", string.Empty).Replace('\\', '/'); // 获取文件名
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        imageUrls.Add(fileName); // 添加到背景图片列表
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception($"[GetImageUrls]:{ex}");
            }
            return imageUrls;
        }

        // GET api/<FileNameController>/5
        [HttpGet("{model}/{function}")]
        public IEnumerable<string> Get(string model, string function)
        {
            List<string> imageUrls = new List<string>();
            try
            {
                string path = $"{Environment.CurrentDirectory}\\wwwroot\\Images\\{model}\\{function}";
                string[] files = Directory.GetFiles(path, "*.*")
                .Where(f => f.EndsWith(".jpg", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".jpeg", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".png", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".webp", StringComparison.OrdinalIgnoreCase)
                         || f.EndsWith(".bmp", StringComparison.OrdinalIgnoreCase))
                .ToArray();

                foreach (var file in files)
                {
                    string fileName = file.Replace($"{Environment.CurrentDirectory}\\wwwroot\\Images\\", string.Empty).Replace('\\', '/'); // 获取文件名
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        imageUrls.Add(fileName); // 添加到背景图片列表
                    }
                }
            }
            catch (Exception ex)
            {
                LogHelper.Exception($"[GetImageUrls]:{ex}");
            }
            return imageUrls;
        }

        // POST api/<FileNameController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/<FileNameController>/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/<FileNameController>/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
